<?php

class RoleAction extends CommonAction {

    public function _initialize() {
        parent::_initialize();
    }

    public function index() {
        $Role = M('Role');
        $map = array();

        $rows = $Role->where($map)
                //->join('Developer on Developer.developer_id = User.developer_id')
                ->join('Access on Access.role_id = Role.id and node_id <> 1 ')
                ->join('Node on Node.id = Access.node_id')
                ->join('RoleUser on RoleUser.role_id = Role.id')
                ->join('User on RoleUser.user_id = User.user_id')
                ->group('Role.id')
                ->order('id ASC')
                ->field('Role.*,group_concat(distinct Node.title) AS Nodelist,group_concat(distinct User.account)  AS Userlist')
                //->cache(true,'360000','','Role')
                ->select();
        $this->assign('rows', $rows);
        $this->display('index');
    }

    public function nodeList() {
        $id = $this->_get('id');
        if (!empty($id)) {
            $map['role_id'] = array('eq', $id);
            $map['pid'] = array('neq', 0);
            $Access = M("Access");
            $hadNodeList = $Access->where($map)->field('node_id')->cache(true,'360000','','Access')->select();

            foreach ($hadNodeList as $nodeId) {
                $nodeL[] = $nodeId['node_id'];
            }

            $Node = M("Node");
            $Nodelist = $Node->where('pid = 1')->field('id,title')->cache(true,'360000','','Node')->select();

            foreach ($Nodelist as $key => $node) {
                if (in_array($node['id'], $nodeL)) {
                    $Nodelist[$key]['checked'] = '1';
                } else {
                    $Nodelist[$key]['checked'] = '0';
                }
            }
            $Role = M('Role');
            $name = $Role->cache(true,'360000','','Role')->getById($id);
            $this->assign('id', $id);
            $this->assign('name', $name['name']);
            $this->assign('NodeList', $Nodelist);
        }
        $this->display();
    }

    public function userList() {
        $User = M("User");
        $userList = $User->field('user_id,account')->cache(true,'360000','','User')->select();

        $id = $this->_get('id');
        if (!empty($id)) {
            $map['role_id'] = array('eq', $id);
            $RoleUser = M("RoleUser");
            $hadUserList = $RoleUser->where($map)->field('user_id')->cache(true,'360000','','RoleUser')->select();
        }
        foreach ($hadUserList as $userId) {
            $userL[] = $userId['user_id'];
        }
        foreach ($userList as $key => $user) {
            if (in_array($user['user_id'], $userL)) {
                $userList[$key]['checked'] = '1';
            } else {
                $userList[$key]['checked'] = '0';
            }
        }
        $Role = M('Role');
        $name = $Role->cache(true,'360000','','Role')->getById($id);
        $this->assign('id', $id);
        $this->assign('name', $name['name']);
        $this->assign('userList', $userList);
        $this->display();
    }

    public function addUser2Role() {

        $roleID = $this->_post('id');
        $listUserID = $this->_post('user');
        $RoleUser = M('RoleUser');
        $this->clearRedisByAction(C('REDIS_SITE').'RoleUser');
        $this->clearRedisByAction(C('REDIS_SITE').'Role');
        $RoleUser->where('role_id =' . $roleID)->delete();
        //$listUserIDArr = explode(',', $listUserID);
        $listUserIDArr = $listUserID;
        for ($i = 0; $i < count($listUserIDArr); $i++) {
            $data = '';
            $data['role_id'] = $roleID;
            $data['user_id'] = $listUserIDArr[$i];
            $RoleUser->add($data);
        }
        $this->success('数据更新成功！');
    }

    public function addRole2Access() {
        $roleID = $this->_post('id');
        $hadNodeID = $this->_post('node');
        $Access = M('Access');
        $this->clearRedisByAction(C('REDIS_SITE').'Role');
        $this->clearRedisByAction(C('REDIS_SITE').'Access');
        $Access->where('role_id =' . $roleID . '')->delete();

        $data = '';
        $data['role_id'] = $roleID;
        $data['node_id'] = 1;
        $data['level'] = 1;
        $data['pid'] = 0;
        $Access->add($data);

        //$hadNodeIDArr = explode(',', $hadNodeID);
        $hadNodeIDArr = $hadNodeID;

        for ($i = 0; $i < count($hadNodeIDArr); $i++) {
            $data = '';
            $data['role_id'] = $roleID;
            $data['node_id'] = $hadNodeIDArr[$i];
            $data['level'] = 2;
            $data['pid'] = 1;
            $Access->add($data);
        }
        $this->success('数据更新成功！');
    }

}